package com.rds.document.service.impl;

import com.rds.common.annotation.DataScope;
import com.rds.common.core.domain.TreeSelect;
import com.rds.common.core.domain.entity.Folder;
import com.rds.common.utils.StringUtils;
import com.rds.common.utils.uuid.SnowFlakeUtils;
import com.rds.system.mapper.FolderMapper;
import com.rds.document.service.IFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 文件夹管理 服务实现
 * @author ruoyi
 */
@Service
public class FolderServiceImpl implements IFolderService
{
    @Autowired
    private FolderMapper folderMapper;

    /**
     * 查询管理数据
     *
     * @param folder 信息
     * @return 信息集合
     */
    @Override
    @DataScope(userStr = "read")
    public List<Folder> selectFolderList(Folder folder)
    {
        return folderMapper.selectFolderList(folder);
    }

    public Folder selectRdaFolderById(Long id){
        return folderMapper.selectRdaFolderById(id);
    }

    public List<Folder> selectRdaFolderByPId(Long parentid){
        return folderMapper.selectRdaFolderByPId(parentid);
    }

    @Override
    public Folder selectRdaFolderByObjectId(String objectId) {
        return folderMapper.selectRdaFolderByObjectId(objectId);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param folders 列表
     * @return 树结构列表
     */
    @Override
    public List<Folder> buildFolderTree(List<Folder> folders)
    {
        List<Folder> returnList = new ArrayList<Folder>();
        List<Long> folderList = new ArrayList<Long>();
        for (Folder folder : folders)
        {
            folderList.add(folder.getId());
        }
        for (Iterator<Folder> iterator = folders.iterator(); iterator.hasNext();)
        {
            Folder folder = (Folder) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!folderList.contains(folder.getParentid()))
            {
                recursionFn(folders, folder);
                returnList.add(folder);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = folders;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param folders 列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildFolderTreeSelect(List<Folder> folders)
    {
        List<Folder> folderTrees = buildFolderTree(folders);
        return folderTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<Folder> list, Folder t)
    {
        // 得到子节点列表
        List<Folder> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Folder tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Folder> getChildList(List<Folder> list, Folder t)
    {
        List<Folder> tlist = new ArrayList<Folder>();
        Iterator<Folder> it = list.iterator();
        while (it.hasNext())
        {
            Folder n = (Folder) it.next();
            if (StringUtils.isNotNull(n.getParentid()) && n.getParentid().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Folder> list, Folder t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 查询文件所有下级文件夹
     *
     * @param folderId 信息
     * @return 所有信息
     */
    @Override
    public Set<Long> getAllFolderId(Long folderId)
    {
        List<Folder> folderList=folderMapper.getAllFolderId(folderId);
        return getAllFolderIdStr(folderList);
    }

    /**
     * 查询文件所有下级文件夹
     *
     * @param projectId 信息
     * @return 所有信息
     */
    @Override
    public Set<Long> getAllProjectId(String projectId)
    {
        List<Folder> folderList=folderMapper.getAllProjectId(Long.parseLong(projectId));
        return getAllFolderIdStr(folderList);
    }


    /**
     * 递归获取父节点下面的所有子节点
     * @param folderList 数据
     * @return 父节点ID+所有子节点的ID字符串，使用逗号隔开
     */
    private Set<Long> getAllFolderIdStr(List<Folder> folderList){
        Set<Long> idStrList = new HashSet<>();
        for(Folder folder: folderList){
            idStrList.add(folder.getId());
            //如果子节点部不为空,继续遍历
            if(folder.getFolderList() != null && !folder.getFolderList().isEmpty()){
                idStrList.addAll(getAllFolderIdStr(folder.getFolderList()));
            }
        }
        return idStrList;
    }


    /**
     * 新增
     *
     * @param folder
     * @return 结果
     */
    @Override
    public int insertFolder(Folder folder)
    {
        //folder.setCreateTime(DateUtils.getNowDate());
        folder.setId(SnowFlakeUtils.nextId());
        return folderMapper.insertFolder(folder);
    }

    /**
     * 修改command命令
     * @param folder
     * @return 结果
     */
    @Override
    public int updateFolder(Folder folder)
    {
        return folderMapper.updateFolder(folder);
    }

    /**
     * 批量删除
     *
     * @param ids 需要删除的ID
     * @return 结果
     */
    @Override
    public int deleteFolderByIds(Long[] ids)
    {
        return folderMapper.deleteFolderByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return 结果
     */
    @Override
    public int deleteFolderById(Long id)
    {
        return folderMapper.deleteFolderById(id);
    }

    @Override
    public List<Folder> selectRdaFolderList(Folder folder) {
        return folderMapper.selectRdaFolderList(folder);
    }

}
